package com.youha.web;


import com.youha.listener.WebApplicationContextUtils;
import com.youha.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author youha
 * @create 2021-08-16 20:01
 */
public class UserServlet extends HttpServlet{

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        ServletContext servletContext = req.getServletContext();

//        ApplicationContext context = WebApplicationContextUtils.getApplicationContext(servletContext);

//        ApplicationContext context = (ApplicationContext) servletContext.getAttribute("context");

        WebApplicationContext context = org.springframework.web.context.support.WebApplicationContextUtils.getWebApplicationContext(servletContext);

        UserService userService = (UserService) context.getBean("userService");

        userService.save();
    }
}
